<?php
require_once("lib/db_connection.php");
require_once("lib/controller.php");

$company_id = $_SESSION['company_id'];
$all_or_select = $_POST['hfAllOrSelect'];
$report_type = $_POST['hfReportType'];
$date_from = $_POST['hfDateFrom'];
$date_to = $_POST['hfDateTo'];

/*$company_id = 1;
$all_or_select = "All";
$report_type = 0;
$date_from = "2009-10-21";
$date_to = "2010-10-28";*/

//echo $company_id."<br/>";
//echo $all_or_select."<br/>";
//echo $report_type."<br/>";
//echo $date_from."<br/>";
//echo $date_to."<br/>";
$result_comp = mysql_query("SELECT * FROM company_tbl WHERE company_id='".$company_id."'")
or die("SELECT COMPANY ".mysql_error());
$row_comp = mysql_fetch_array($result_comp);

function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }
?>
<html>
<head>
<style type="text/css">
table {
font-family:Times New Roman;
font-size:14px;
}
</style>
</head>
<body style='background-color:#FFFFFF' onLoad='window.print()'>
<table border='0' width='810' style='margin-left:auto;margin-right:auto;'>
<tr>
 <th width='100%' style='text-align:center' colspan='3'><?php echo $row_comp['company_name']?></th>
</tr>
<tr>
 <th width='100%' style='text-align:center' colspan='3'><?php echo $row_comp['address']?></th>
</tr>
<tr>
 <td width='33%' style='text-align:left;'>Contact No:<?php echo $row_comp['contact_no']?></td>
 <td width='34%' style='text-align:center'><?php echo $row_comp['website']?></td>
 <td width='33%' style='text-align:right;'>Fax No:<?php echo $row_comp['fax_no']?></td>
</tr>
</table>
<?php
if($all_or_select=='All')//IF USER SELECTED ALL VEHICLES V2.0
{
	?>
	<div style="width: 100%; text-align: center;"><img border="0" src="image/summary_report.png" width="810" height="30" /></div>
  <!--<div style='padding-left:129px;width:810;text-align: right;'>Includes Data From:<?php echo $date_from ?> To:<?php echo $date_to ?></div>-->
  <table border='0' width='810' style='margin-left:auto;margin-right:auto'>
	<tr>
  <td style='text-align:right'>Date From:<?php echo $date_from ?></td>
	</tr>
	<tr>
	<td style='text-align:right'>To:<?php echo $date_to ?></td>
	</tr>
	</table>

  <br />
	<br />
	<?php
	$my_controller = new myController();

	//WORK SUMMARY
	if($report_type=='0' || $report_type=='1')
	{
		$query_ws_veh = "SELECT vehicle_id FROM vehicle_tbl WHERE company_id='".$company_id."' ORDER BY vehicle_id ASC";
		$result_ws_veh = mysql_query($query_ws_veh) or die("SELECT WS VEHICLES ".mysql_error());

		?>
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_work_summary.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<th>Plate</th>
		<th>Year</th>
		<th>Make</th>
		<th>Model</th>
		<th>Date</th>
		<th>Serviced By</th>
		<th>Description</th>
		<th>Labor</th>
		<th>Parts</th>
		<th>Misc</th>
		<th>Disc</th>
		<th>Total</th>
		<?php
		$WS_TOTAL = 0;
		while($row_ws_veh = mysql_fetch_array($result_ws_veh))
		{
			$query_ws  = "SELECT * FROM veh_workorder_tbl WHERE vehicle_id='".$row_ws_veh['vehicle_id']."' AND complete=1 AND completion_date>='".$date_from."' AND completion_date<='".$date_to."' ORDER BY completion_date ASC";
			$result_ws = mysql_query($query_ws) or die("SELECT WORK SUMMARY ".mysql_error());

			$prev_veh_id = -1;
			while($row_ws = mysql_fetch_array($result_ws))
			{
				$ws_veh_details = array();
				//$ws_veh_details = array("plate"=>"-",
				//"year"=>"-",
				//"make"=>"-",
				//"model"=>"-");
				$ws_veh_id = $row_ws['vehicle_id'];

				//if($prev_veh_id!=$ws_veh_id)
				//{
				$ws_veh_details = $my_controller->getVehicleDetailsById($ws_veh_id);
				//}
				$WS_TOTAL += $row_ws['total'];
				?>
				<tr>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["plate"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["year"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["make"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["model"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_ws['completion_date'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_ws['service_name'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_ws['description'] ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_ws['labor']) ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_ws['parts']) ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_ws['misc']) ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_ws['discount']) ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_ws['total']) ?>&nbsp;</td>
				</tr>
				<?php
				$prev_veh_id = $ws_veh_id;
			}
		}
		?>
		<tr>
			<td style="text-align:right;font-weight:bold;" colspan="11">&nbsp;TOTAL&nbsp;</td>
			<td style="text-align:right;font-weight:bold;">&nbsp;<?php echo commafy(number_format($WS_TOTAL,2)) ?>&nbsp;</td>
		</tr>
		</table>
		<br />
		<br />
		<?php
	}



	//FUEL
	if($report_type=='0' || $report_type=='2')
	{
		?>
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_fuel.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<th>Plate</th>
		<th>Year</th>
		<th>Make</th>
		<th>Model</th>
		<th>Date</th>
		<th>Driver</th>
		<th>Brand</th>
		<th>Total</th>
		<?php
			$query_fuel  = "SELECT * FROM veh_fuel_tbl WHERE company_id='".$company_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY vehicle_id ASC, date ASC";
			$result_fuel = mysql_query($query_fuel) or die("SELECT FUEL ".mysql_error());

			$prev_veh_id = -1;
			$FUEL_TOTAL = 0;
			while($row_fuel = mysql_fetch_array($result_fuel))
			{

				$ws_veh_details = array();
				//$ws_veh_details = array("plate"=>"-",
				//"year"=>"-",
				//"make"=>"-",
				//"model"=>"-");
				$fuel_veh_id = $row_fuel['vehicle_id'];

				//if($prev_veh_id!=$fuel_veh_id)
				//{
				$ws_veh_details = $my_controller->getVehicleDetailsById($fuel_veh_id);
				//}
				$FUEL_TOTAL += $row_fuel['tot_cost'];
				?>
				<tr>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["plate"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["year"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["make"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["model"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_fuel['date'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_fuel['driver'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_fuel['brand'] ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_fuel['tot_cost']) ?>&nbsp;</td>
				</tr>
				<?php
				$prev_veh_id = $fuel_veh_id;
			}
		?>
		<tr>
			<td style="text-align:right;font-weight:bold;" colspan="7">&nbsp;TOTAL&nbsp;</td>
			<td style="text-align:right;font-weight:bold;">&nbsp;<?php echo commafy(number_format($FUEL_TOTAL,2)) ?>&nbsp;</td>
		</tr>
		</table>
		<br />
		<br />
		<?php
	}



	//EXPENSE
	if($report_type=='0' || $report_type=='3')
	{
		$query_exp_veh = "SELECT vehicle_id FROM vehicle_tbl WHERE company_id='".$company_id."' ORDER BY vehicle_id ASC";
		$result_exp_veh = mysql_query($query_exp_veh) or die("SELECT EXP VEHICLES ".mysql_error());
		?>
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_other_expenses.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
			<th>Plate</th>
			<th>Year</th>
			<th>Make</th>
			<th>Model</th>
			<th>Date</th>
			<?php/*<th>Description</th>*/?>
			<th>Expense Type</th>
			<th>Amount</th>
		<?php
		$EXP_TOTAL = 0;
		while($row_exp_veh = mysql_fetch_array($result_exp_veh))
		{
			$query_expense  = "SELECT * FROM veh_expense_tbl WHERE vehicle_id='".$row_exp_veh['vehicle_id']."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY vehicle_id ASC, date ASC";
			$result_expense = mysql_query($query_expense) or die("SELECT EXPENSE ".mysql_error());

			$prev_veh_id = -1;
			while($row_expense = mysql_fetch_array($result_expense))
			{

				$exp_veh_details = array();
				//$exp_veh_details = array("plate"=>"-",
				//"year"=>"-",
				//"make"=>"-",
				//"model"=>"-");
				$exp_veh_id = $row_expense['vehicle_id'];

				//if($prev_veh_id!=$exp_veh_id)
				//{
				$exp_veh_details = $my_controller->getVehicleDetailsById($exp_veh_id);
				//}
				$EXP_TOTAL += $row_expense['amount'];
				?>
				<tr>
					<td style="text-align:center;">&nbsp;<?php echo $exp_veh_details["plate"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $exp_veh_details["year"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $exp_veh_details["make"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $exp_veh_details["model"] ?>&nbsp;</td>
					<td style='text-align:center'><?php echo $row_expense['date']?>&nbsp;</td>
					<?php /*<td style='text-align:center'><?php echo $row_expense['description']?>&nbsp;</td>*/?>
          <td style='text-align:center'><?php echo $row_expense['type']?>&nbsp;</td>
					<td style='text-align:right'><?php echo commafy($row_expense['amount'])?>&nbsp;</td>
				</tr>
				<?php
				$prev_veh_id = $exp_veh_id;
			}
		}
		?>
		<tr>
			<td style="text-align:right;font-weight:bold;" colspan="6">&nbsp;TOTAL&nbsp;</td>
			<td style="text-align:right;font-weight:bold;">&nbsp;<?php echo commafy(number_format($EXP_TOTAL,2)) ?>&nbsp;</td>
		</tr>
		</table>
		<br />
		<br />
		<?php
	}



	//INCIDENT
	if($report_type=='0' || $report_type=='4')
	{
		?>
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_incidents.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<th>Plate</th>
		<th>Year</th>
		<th>Make</th>
		<th>Model</th>
		<th>Date</th>
		<th>Description</th>
		<th>Incident Type</th>
    <th>Driver</th>
		<th>Details</th>
		<th>Cost</th>
		<?php
			$query_incident = "SELECT * FROM incident_cost_tbl WHERE company_id='".$company_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY vehicle_id ASC, date ASC";
			$result_incident = mysql_query($query_incident) or die("SELECT INCIDENT COST ".mysql_error());

			$prev_veh_id = -1;
			$INCIDENT_TOTAL = 0;
			while($row_incident = mysql_fetch_array($result_incident))
			{
				$ws_veh_details = array();
				//$ws_veh_details = array("plate"=>"-",
				//"year"=>"-",
				//"make"=>"-",
				//"model"=>"-");
				$fuel_veh_id = $row_incident['vehicle_id'];

				//if($prev_veh_id!=$fuel_veh_id)
				//{
				$ws_veh_details = $my_controller->getVehicleDetailsById($fuel_veh_id);
				//}

				$result_driver = mysql_query("select driver from veh_incident_tbl where incident_id = '".$row_incident['incident_id']."'");
				$row_driver = mysql_fetch_array($result_driver);

				$INCIDENT_TOTAL += $row_incident['amount'];
				?>
				<tr>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["plate"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["year"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["make"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["model"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_incident['date'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_incident['desc_main'] ?>&nbsp;</td>
          <td style="text-align:center;">&nbsp;<?php echo $row_incident['type'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_driver['driver'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_incident['description'] ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_incident['amount']) ?>&nbsp;</td>
				</tr>
				<?php
				$prev_veh_id = $fuel_veh_id;
			}
		?>
		<tr>
			<td style="text-align:right;font-weight:bold;" colspan="9">&nbsp;TOTAL&nbsp;</td>
			<td style="text-align:right;font-weight:bold;">&nbsp;<?php echo commafy(number_format($INCIDENT_TOTAL,2)) ?>&nbsp;</td>
		</tr>
		</table>
		<br />
		<br />
		<?php
	}




	//FOR DECOMMISSIONED VEHICLES
	if($report_type=='0' || $report_type=='5')
	{
		?>
		<div style="width: 100%; text-align: center;"><img border="0" src="image/decommission_history.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<th>Plate</th>
		<th>Year</th>
		<th>Make</th>
		<th>Model</th>
		<th>Date</th>
		<th>Reason</th>
		<?php
			$query_decommission = "SELECT autoid,veh_id,dec_date,reason FROM veh_decommission WHERE company_id ='".$company_id ."' AND dec_date>='".$date_from."' AND dec_date<='".$date_to."' ORDER BY veh_id ASC, dec_date ASC";
			$result_decommission = mysql_query($query_decommission) or die("SELECT INCIDENT COST ".mysql_error());

			$prev_veh_id = -1;
			while($row_decommission = mysql_fetch_array($result_decommission))
			{
				$ws_veh_details = array();
				//$ws_veh_details = array("plate"=>"-",
				//"year"=>"-",
				//"make"=>"-",
				//"model"=>"-");
				$fuel_veh_id = $row_decommission['veh_id'];

				//if($prev_veh_id!=$fuel_veh_id)
				//{
				$ws_veh_details = $my_controller->getVehicleDetailsById($fuel_veh_id);
				//}

				?>
				<tr>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["plate"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["year"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["make"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ws_veh_details["model"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_decommission['dec_date'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_decommission['reason'] ?>&nbsp;</td>
				</tr>
				<?php
				$prev_veh_id = $fuel_veh_id;
			}
		?>
		</table>
		<br />
		<br />
		<?php
    }
    
    
    
    
    //INSURANCE
	if($report_type=='0' || $report_type=='6')
	{
		$query_ins_veh = "SELECT vehicle_id FROM vehicle_tbl WHERE company_id='".$company_id."' ORDER BY vehicle_id ASC";
		$result_ins_veh = mysql_query($query_ins_veh) or die("SELECT INS VEHICLES ".mysql_error());

		?>
		<div style="width: 100%; text-align: center;"><img border="0" src="image/insurance_report.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
		<th width='10%' rowspan='2'>Plate</th>
		<th width='10%' rowspan='2'>Year</th>
		<th width='10%' rowspan='2'>Make</th>
		<th width='10%' rowspan='2'>Model</th>
		<th width='10%' rowspan='2'>Policy No</th>
		<th width='10%' rowspan='2'>Insurance Company</th>
		<th width='20%' colspan='2'>Period of Cover</th>
		<th width='10%' rowspan='2'>Total Sum Insured</th>
		<th width='10%' rowspan='2'>Total Premium</th>
		</tr>
		<tr>
 		<th width='10%'>From</th>
 		<th width='10%'>To</th>
		</tr>
		<?php
		$INS_TOTAL = 0;
		while($row_ins_veh = mysql_fetch_array($result_ins_veh))
		{
			$query_ins  = "SELECT * FROM veh_insurance_dynamic_tbl WHERE vehicle_id='".$row_ins_veh['vehicle_id']."' AND cover_date>='".$date_from."' AND cover_date<='".$date_to."' ORDER BY cover_date ASC";
			$result_ins = mysql_query($query_ins) or die("SELECT INSURANCE ".mysql_error());

			$prev_veh_id = -1;
			while($row_ins = mysql_fetch_array($result_ins))
			{
				$ins_veh_details = array();
				//$ins_veh_details = array("plate"=>"-",
				//"year"=>"-",
				//"make"=>"-",
				//"model"=>"-");
				$ins_veh_id = $row_ins['vehicle_id'];

				//if($prev_veh_id!=$ins_veh_id)
				//{
				$ins_veh_details = $my_controller->getVehicleDetailsById($ins_veh_id);
				//}
				//$INS_TOTAL += $row_ins['total'];
				?>
				<tr>
					<td style="text-align:center;">&nbsp;<?php echo $ins_veh_details["plate"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ins_veh_details["year"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ins_veh_details["make"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $ins_veh_details["model"] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_ins['policy_num'] ?>&nbsp;</td>
					<td style="text-align:center;">&nbsp;<?php echo $row_ins['company'] ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo $row_ins['cover_date'] ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo $row_ins['renewal_date'] ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_ins['tot_amt']) ?>&nbsp;</td>
					<td style="text-align:right;">&nbsp;<?php echo commafy($row_ins['tot_price']) ?>&nbsp;</td>
				</tr>
				<?php
				$prev_veh_id = $ins_veh_id;
			}
		}
		?>
		</table>
		<br />
		<br />
		<?php
	}

}
/*if($all_or_select=='All')//IF USER SELECTED ALL VEHICLES V1.0
{
	$query_vehicles = "SELECT * FROM vehicle_tbl WHERE company_id='".$company_id."'";
	$result_vehicles = mysql_query($query_vehicles);
	$wo_tot = 0;
	$fuel_tot = 0;
	$expense_tot = 0;
	if($report_type!='5')
	{
?>
	<div style="width: 100%; text-align: center;"><img border="0" src="image/summary_report.png" width="810" height="23" /></div>
	<br /><br />
	<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
	<tr>
		<th>PLATE NO.</th>
		<th>YEAR</th>
		<th>MAKE</th>
		<th>MODEL</th>
<?php
	if($report_type=='0' || $report_type=='1')
	{
?>
		<th>WORK SUMMARY</th>
<?php
	}
	if($report_type=='0' || $report_type=='2')
	{
?>
		<th>FUEL</th>
<?php
	}
	if($report_type=='0' || $report_type=='3')
	{
?>
		<th>EXPENSE</th>
<?php
	}
	if($report_type=='0' || $report_type=='4')
	{
?>
		<th>INCIDENT</th>
<?php
	}
?>
	</tr>
<?php
	while($row_vehicles = mysql_fetch_array($result_vehicles))
	{
	$veh_id = $row_vehicles['vehicle_id'];
	$plate = $row_vehicles['veh_num'];
?>
	<tr>
		<td style='text-align:center;width:121px'><?php echo $plate?></td>
		<td style='text-align:center;width:121px'><?php echo $row_vehicles['year']?></td>
		<td style='text-align:center;width:121px'><?php echo $row_vehicles['manufacturer_name']?></td>
		<td style='text-align:center;width:121px'><?php echo $row_vehicles['model_name']?></td>


<?php
		if($report_type=='0' || $report_type=='1')
		{
			$query_wo = "SELECT SUM(total) as wo_total FROM veh_workorder_tbl WHERE vehicle_id='".$veh_id."' AND completion_date>='".$date_from."' AND completion_date<='".$date_to."' ORDER BY completion_date";
			$result_wo = mysql_query($query_wo) or die("WORK ORDER ".mysql_error());
			$row_wo = mysql_fetch_array($result_wo);
			$wo_tot += $row_wo['wo_total'];
?>
		<td style='text-align:right;width:230px'>&nbsp;<?php echo commafy($row_wo['wo_total'])?></td>
<?php
		}
		if($report_type=='0' || $report_type=='2')
		{
			$query_fuel = "SELECT SUM(tot_cost) as fuel_total FROM veh_fuel_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
			$result_fuel = mysql_query($query_fuel) or die("WORK ORDER ".mysql_error());
			$row_fuel = mysql_fetch_array($result_fuel);
			$fuel_tot += $row_fuel['fuel_total'];
?>
		<td style='text-align:right;width:230px'>&nbsp;<?php echo commafy($row_fuel['fuel_total'])?></td>
<?php
		}
		if($report_type=='0' || $report_type=='3')
		{
			$query_expense = "SELECT SUM(amount) as expense_total FROM veh_expense_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
			$result_expense = mysql_query($query_expense) or die("WORK ORDER ".mysql_error());
			$row_expense = mysql_fetch_array($result_expense);
			$expense_tot += $row_expense['expense_total'];
?>
		<td style='text-align:right;width:230px'>&nbsp;<?php echo commafy($row_expense['expense_total'])?></td>
<?php
		}
		if($report_type=='0' || $report_type=='4')
		{
			$query_incident = "SELECT SUM(amount) as incident_total FROM incident_cost_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
			$result_incident = mysql_query($query_incident) or die("INCIDENT ".mysql_error());
			$row_incident = mysql_fetch_array($result_incident);
			$incident_tot += $row_incident['incident_total'];
?>
		<td style='text-align:right;width:230px'>&nbsp;<?php echo commafy($row_incident['incident_total'])?></td>
<?php
		}
?>


	</tr>
<?php
	}
?>
	<tr>
		<td colspan='4' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>TOTAL</td>
	<?php
	}
		if($report_type=='0' || $report_type=='1')
		{
?>
		<td style='background-color:#C0C0C0;text-align:right'>&nbsp;<?php echo commafy($wo_tot)?></td>
<?php
		}
		if($report_type=='0' || $report_type=='2')
		{
?>
		<td style='background-color:#C0C0C0;text-align:right'>&nbsp;<?php echo commafy($fuel_tot)?></td>
<?php
		}
		if($report_type=='0' || $report_type=='3')
		{
?>
		<td style='background-color:#C0C0C0;text-align:right'>&nbsp;<?php echo commafy($expense_tot)?></td>
<?php
		}
		if($report_type=='0' || $report_type=='4')
		{
?>
		<td style='background-color:#C0C0C0;text-align:right'>&nbsp;<?php echo commafy($incident_tot)?></td>
<?php
		}
?>
	</tr>
	</table>
<?php


	//FOR DECOMMISSIONED VEHICLES
	if($report_type=='0' || $report_type=='5')
	{
    $result_vehicles2 = mysql_query($query_vehicles);
	while($row_veh_dec = mysql_fetch_array($result_vehicles2))
	{
	$year = $row_veh_dec['year'];
	$make = $row_veh_dec['manufacturer_name'];
	$model = $row_veh_dec['model_name'];
	$plate = $row_veh_dec['veh_num'];
	$veh_id2 = $row_veh_dec['vehicle_id'];

	$query_list = "SELECT autoid,dec_date,reason FROM veh_decommission WHERE veh_id='".$veh_id2."' AND dec_date>='".$date_from."' AND dec_date<='".$date_to."' ORDER BY dec_date DESC";
	$result_list = mysql_query($query_list) or die("LIST ".mysql_error());

	$reason_count = mysql_num_rows($result_list);
	if($reason_count>0)
	{
?>
	<br/>
	<table id='veh_dec' border='0' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<td style='font-weight:bold;padding-left:10px;width:6%'><?php echo $plate ?></td>
			<td style='font-weight:bold;width:94%'><?php echo $year."&nbsp;".$make."&nbsp;".$model ?></td>
		</tr>
	</table>


		<div style="width: 100%; text-align: center;"><img border="0" src="image/decommission_history.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<th style='width:15%'>Date</th>
			<th style='width:85%'>Reason</th>
		</tr>
<?php
		while($row_list = mysql_fetch_array($result_list))
		{
?>
		<tr>
			<td style='text-align:center'><?php echo $row_list['dec_date']?></td>
			<td style='padding-left:10px;padding-right:10px;'><?php echo $row_list['reason']?></td>
		</tr>
<?php
		}
?>
		</table>
<?php
		}
}
}
}
*/
?>




<?php
if($all_or_select!='All')//IF USER SELECTED A VEHICLE
{
	//$query_vehicles = "SELECT * FROM vehicle_tbl WHERE company_id='".$company_id."'";
	$query_vehicles = "SELECT * FROM vehicle_tbl WHERE vehicle_id='".$all_or_select."'";
	$result_vehicles = mysql_query($query_vehicles) or die("VEHICLES ".mysql_error());
	$row_vehicles = mysql_fetch_array($result_vehicles);
?>
	<div style="width: 100%; text-align: center;"><img border="0" src="image/summary_report.png" width="810" height="30" /></div>
	<table id='veh_info' border='0' width='810' style='margin-left:auto;margin-right:auto'>
	<tr>
	<td><b>Plate No:&nbsp;<?php echo $row_vehicles['veh_num']?></b></td>
  <td style='text-align:right'>Date From:<?php echo $date_from ?></td>
	</tr>
 	<tr>
 	<td><b>Model:&nbsp;<?php echo $row_vehicles['year']?>&nbsp;<?php echo $row_vehicles['manufacturer_name']?>&nbsp;<?php echo $row_vehicles['model_name']?></b></td>
 	<td style='text-align:right'>To:<?php echo $date_to ?></td>
 	</tr>
	</table>
<?php
	$veh_id = $row_vehicles['vehicle_id'];
	if($report_type=='0' || $report_type=='1')
	{
		$wo_total = 0;
		$query_wo = "SELECT * FROM veh_workorder_tbl WHERE vehicle_id='".$veh_id."' AND complete=1 AND completion_date>='".$date_from."' AND completion_date<='".$date_to."' ORDER BY completion_date";
		$result_wo = mysql_query($query_wo) or die("WORK ORDER ".mysql_error());
?>
		<br />
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_work_summary.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<th width='40'>Date</th>
			<th width='40'>Reference No</th>
			<th width='270'>Description</th>
			<th width='30'>Km/Hrs</th>
			<th width='270'>Serviced By</th>
			<th width='30'>Labor</th>
			<th width='30'>Parts</th>
			<th width='30'>Misc</th>
			<th width='30'>Disc</th>
			<th width='40'>Total</th>
		</tr>
<?php
		while($row_wo = mysql_fetch_array($result_wo))
		{
		$wo_total+=$row_wo['total'];
		$wo_labor+=$row_wo['labor'];
		$wo_part+=$row_wo['parts'];
		$wo_misc+=$row_wo['misc'];
		$wo_disc+=$row_wo['discount'];
    $track_by = "";
    if($row_wo['track_by']=='distance')
    $track_by = "Km";
    if($row_wo['track_by']=='hours')
    $track_by = "Hrs";
?>
		<tr>
			<td width='40'>&nbsp;<?php echo $row_wo['completion_date']?>&nbsp;</td>
			<td width='40'>&nbsp;<?php echo $row_wo['order_num']?>&nbsp;</td>
			<td width='270'>&nbsp;<?php echo $row_wo['description']?>&nbsp;</td>
			<td width='30' style='text-align:right'>&nbsp;<?php echo $row_wo['odometer']?><?php echo $track_by?>&nbsp;</td>
			<td width='270'>&nbsp;<?php echo $row_wo['service_name']?>&nbsp;</td>
			<td width='30' style='text-align:right'>&nbsp;<?php echo commafy($row_wo['labor'])?>&nbsp;</td>
			<td width='30' style='text-align:right'>&nbsp;<?php echo commafy($row_wo['parts'])?>&nbsp;</td>
			<td width='30' style='text-align:right'>&nbsp;<?php echo commafy($row_wo['misc'])?>&nbsp;</td>
			<td width='30' style='text-align:right'>&nbsp;<?php echo commafy($row_wo['discount'])?>&nbsp;</td>
			<td width='40' style='text-align:right'>&nbsp;<?php echo commafy($row_wo['total'])?>&nbsp;</td>
		</tr>

<?php
		}
?>
		<tr>
			<td colspan='5' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($wo_labor,2))?></td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($wo_part,2))?></td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($wo_misc,2))?></td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($wo_disc,2))?></td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($wo_total,2))?></td>
		</tr>
		</table>
<?php
	}


	if($report_type=='0' || $report_type=='2')
	{
		$fuel_total = 0;
		$query_fuel = "SELECT * FROM veh_fuel_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		$result_fuel = mysql_query($query_fuel) or die("WORK ORDER ".mysql_error());
?>
		<br />
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_fuel.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<th>Date</th>
			<th>Driver</th>
			<th>Brand</th>
      		<th>Liters</th>
			<th>Price/Li</th>
			<th>Total</th>
			<th>Km/Hrs</th>
			<th>Km/L</th>
		</tr>
<?php
		while($row_fuel = mysql_fetch_array($result_fuel))
		{
		$fuel_total+=$row_fuel['tot_cost'];
		$lit_tot+=$row_fuel['gallons'];
		$mi_tot+=$row_fuel['miles'];
    $track_by = "";
    if($row_fuel['track_by']=='distance')
    $track_by = "Km";
    if($row_fuel['track_by']=='hours')
    $track_by = "Hrs";
    
    //km/l
    $kml = $row_fuel['miles']/$row_fuel['gallons'];
?>
		<tr>
			<td>&nbsp;<?php echo $row_fuel['date']?>&nbsp;</td>
			<td>&nbsp;<?php echo $row_fuel['driver']?>&nbsp;</td>
			<td>&nbsp;<?php echo $row_fuel['brand']?>&nbsp;</td>
      		<td style='text-align:right'>&nbsp;<?php echo $row_fuel['gallons']?>&nbsp;</td>
			<td style='text-align:right'>&nbsp;<?php echo commafy($row_fuel['price'])?>&nbsp;</td>
			<td style='text-align:right'>&nbsp;<?php echo commafy($row_fuel['tot_cost'])?>&nbsp;</td>
			<td style='text-align:right'>&nbsp;<?php echo $row_fuel['miles']?><?php echo $track_by?>&nbsp;</td>
			<td style='text-align:right'>&nbsp;<?php echo $kml ?>&nbsp;</td>
		</tr>

<?php
		}
?>
		<tr>
			<td colspan='4' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy($lit_tot)?></td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($fuel_total,2))?></td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy($mi_tot)?></td>
			<td style='background-color:#C0C0C0;text-align:right'>&nbsp;</td>
		</tr>
		</table>
<?php
	}

	if($report_type=='0' || $report_type=='3')
	{
		$expense_total = 0;
		$query_expense = "SELECT * FROM veh_expense_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		$result_expense = mysql_query($query_expense) or die("WORK ORDER ".mysql_error());
?>
		<br />
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_other_expenses.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<th>Date</th>
			<?php/*<th>Description</th>*/?>
			<th>Expense Type</th>
			<th>Amount</th>
		</tr>
<?php
		while($row_expense = mysql_fetch_array($result_expense))
		{
		$expense_total+=$row_expense['amount'];
?>
		<tr>
			<td><?php echo $row_expense['date']?>&nbsp;</td>
			<?php /*<td><?php echo $row_expense['description']?>&nbsp;</td>*/ ?>
			<td><?php echo $row_expense['type']?>&nbsp;</td>
			<td style='text-align:right'><?php echo commafy($row_expense['amount'])?>&nbsp;</td>
		</tr>

<?php
		}
?>
		<tr>
			<td colspan='2' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($expense_total,2))?></td>
		</tr>
		</table>
<?php
	}
		if($report_type=='0' || $report_type=='4')
		{
		$incident_total = 0;
		//$query_incident = "SELECT * FROM veh_incident_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		//$result_incident = mysql_query($query_incident) or die("INCIDENT ".mysql_error());
		$query_cost = "SELECT * FROM incident_cost_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		$result_cost = mysql_query($query_cost) or die("INCIDENT COST ".mysql_error());

		$query_tot = "SELECT SUM(amount) as cost_total FROM incident_cost_tbl WHERE vehicle_id='".$veh_id."' AND date>='".$date_from."' AND date<='".$date_to."' ORDER BY date";
		$result_tot = mysql_query($query_tot) or die("TOT COST ".mysql_error());
		$row_tot = mysql_fetch_array($result_tot);
		$incident_total += $row_tot['cost_total'];

?>
		<br />
		<div style="width: 100%; text-align: center;"><img border="0" src="image/rep_incidents.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<th>Incident Date</th>
			<th>Description</th>
      <th>Incident Type</th>
			<th>Driver</th>
			<th>Details</th>
			<th>Cost</th>
		</tr>
<?php
		while($row_incident = mysql_fetch_array($result_cost))
		{
			$result_driver = mysql_query("select * from veh_incident_tbl where incident_id = '".$row_incident['incident_id']."'");
			$row_driver = mysql_fetch_array($result_driver);
?>
		<tr>
			<td>&nbsp;<?php echo $row_incident['date']?>&nbsp;</td>
			<td>&nbsp;<?php echo $row_incident['desc_main']?>&nbsp;</td>
			<td>&nbsp;<?php echo $row_incident['type']?>&nbsp;</td>
      <td>&nbsp;<?php echo $row_driver['driver']?>&nbsp;</td>
			<td>&nbsp;<?php echo $row_incident['description']?>&nbsp;</td>
			<td style='text-align:right'>&nbsp;<?php echo commafy($row_incident['amount'])?>&nbsp;</td>
		</tr>
<?php
		}
?>
		<tr>
			<td colspan='5' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($incident_total,2))?></td>
		</tr>
		</table>
<?php
	}

		if($report_type=='0' || $report_type=='5')
		{
		$query_list = "SELECT autoid,dec_date,reason FROM veh_decommission WHERE veh_id='".$veh_id."' AND dec_date>='".$date_from."' AND dec_date<='".$date_to."' ORDER BY dec_date DESC";
		$result_list = mysql_query($query_list) or die("LIST ".mysql_error());

?>
		<br />
		<div style="width: 100%; text-align: center;"><img border="0" src="image/decommission_history.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<th style='width:15%'>Start Date</th>
			<th style='width:15%'>End Date</th>
			<th style='width:70%'>Reason</th>
		</tr>
<?php
		while($row_list = mysql_fetch_array($result_list))
		{
?>
		<tr>
			<td style='text-align:center'><?php echo $row_list['dec_date']?>&nbsp;</td>
			<td style='text-align:center'><?php echo $row_list['end_date']?>&nbsp;</td>
			<td style='padding-left:10px;padding-right:10px;'><?php echo $row_list['reason']?>&nbsp;</td>
		</tr>
<?php
		}
?>
		</table>
<?php
	}


//INSURANCE
if($report_type=='0' || $report_type=='6')
	{
		$query_ins = "SELECT * FROM veh_insurance_dynamic_tbl WHERE vehicle_id='".$veh_id."' AND cover_date>='".$date_from."' AND cover_date<='".$date_to."' ORDER BY cover_date DESC";
		$result_ins = mysql_query($query_ins) or die("INSURANCE ".mysql_error());
?>
		<br />
		<div style="width: 100%; text-align: center;"><img border="0" src="image/insurance_report.png" width="810" height="23"/></div>
		<table border='1' width='810' style='margin-left:auto;margin-right:auto'>
		<tr>
			<th width='20%' rowspan='2'>Policy No</th>
			<th width='20%' rowspan='2'>Insurance Company</th>
			<th width='20%' rowspan='2'>Intermediary</th>
			<th width='20%' colspan='2'>Period of Cover</th>
			<th width='10%' rowspan='2'>Total Sum Insured</th>
			<th width='10%' rowspan='2'>Total Premium</th>
		</tr>
		<tr>
			<th width='10%'>From</th>
			<th width='10%'>To</th>
		</tr>
<?php
		while($row_ins = mysql_fetch_array($result_ins))
		{
		$ins_total+=$row_ins['tot_amt'];
		$ins_price+=$row_ins['tot_price'];
	
?>
		<tr>
			<td width='20%'>&nbsp;<?php echo $row_ins['policy_num']?>&nbsp;</td>
			<td width='20%'>&nbsp;<?php echo $row_ins['company']?>&nbsp;</td>
			<td width='20%'>&nbsp;<?php echo $row_ins['agent']?>&nbsp;</td>
			<td width='10%'>&nbsp;<?php echo $row_ins['cover_date']?>&nbsp;</td>
			<td width='10%'>&nbsp;<?php echo $row_ins['renewal_date']?>&nbsp;</td>
			<td width='20%' style='text-align:right'>&nbsp;<?php echo commafy($row_ins['tot_amt'])?>&nbsp;</td>
			<td width='20%' style='text-align:right'>&nbsp;<?php echo commafy($row_ins['tot_price'])?>&nbsp;</td>
		</tr>

<?php
		}
?>
		<!--<tr>
			<td colspan='5' style='font-weight:bold;background-color:#C0C0C0;text-align:right'>Overall Total</td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($ins_total,2))?></td>
			<td style='background-color:#C0C0C0;text-align:right'><?php echo commafy(number_format($ins_price,2))?></td>
		</tr>-->
		</table>
<?php
	}


}
?>

<p style=" position: relative; bottom: 0; left: 0; width: 100%; text-align: center;"><img border="0" src="image/footer_wo.png" width="135" height="30" style='margin-left:auto;margin-right:auto'/></p>
</body>
</html>